﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DiaosFix.Core
{
    public class RequestsOperator
    {
        private DiaosFixEntities context = new DiaosFixEntities();

        public Response<Requests> FindRequest(int requestId)
        {
            var query = context.Requests.Where(p => p.Id == requestId).SingleOrDefault();
            if (null != query)
            {
                return new Response<Requests>()
                {
                    IsSuccess = true,
                    Message = string.Empty,
                    Item = query
                };
            }
            else 
            {
                return new Response<Requests>()
                {
                    IsSuccess = false,
                    Message = "Cannot find the request",
                    Item = null
                };
            }
        }

        public Response<List<Requests>> FindAllRequest()
        {
            var query = context.Requests.ToList();
            if (null != query)
            {
                return new Response<List<Requests>>()
                {
                    IsSuccess = true,
                    Message = string.Empty,
                    Item = query
                };
            }
            else
            {
                return new Response<List<Requests>>()
                {
                    IsSuccess = false,
                    Message = "Error querying requests list",
                    Item = null
                };
            }
        }

        public Response<Requests> RaiseRequest(string username, int requestAreaId, string requestDescription)
        {
            var uo = new UsersOperator();
            var rao = new RequestAreaOperator();
            var uao = new UserAreaOperator();

            var uoResponse = uo.FindUser(username);
            if (uoResponse.IsSuccess)
            {
                if (uoResponse.Item.IsInRole(PubConstant.UserTypes.User))
                {
                    var reqToken = Guid.NewGuid();
                    var userId = uoResponse.Item.Id;
                    var uaoResponse = uao.GetUsersByArea(requestAreaId);
                    if (uaoResponse.IsSuccess)
                    {
                        var rand = new Random();
                        var index = rand.Next(0, uaoResponse.Item.Count);
                        var assignToId = uaoResponse.Item[index].Id;

                        var req = new Requests()
                        {
                            OpenDate = DateTime.Now,
                            RequestAreaId = requestAreaId,
                            RequestDescription = requestDescription,
                            RequestStatusId = (int)PubConstant.RequestStatus.Open,
                            RaisedBy = userId,
                            AssignedTo = assignToId,
                            Token = reqToken
                        };

                        context.AddToRequests(req);
                        if (context.SaveChanges() > 0)
                        {
                            return new Response<Requests>()
                            {
                                IsSuccess = true,
                                Message = string.Empty,
                                Item = context.Requests.Where(p => p.Token == reqToken).SingleOrDefault()
                            };
                        }
                        else
                        {
                            return new Response<Requests>()
                            {
                                IsSuccess = false,
                                Message = "Error saving data",
                                Item = null
                            };
                        }
                    }
                    else
                    {
                        return new Response<Requests>()
                        {
                            IsSuccess = false,
                            Message = uaoResponse.Message,
                            Item = null
                        };
                    }
                }
                else
                {
                    return new Response<Requests>()
                    {
                        IsSuccess = false,
                        Message = "A Non-user user cannot raise a request",
                        Item = null
                    };
                }
            }
            else
            {
                return new Response<Requests>()
                {
                    IsSuccess = false,
                    Message = "Cannot find user",
                    Item = null
                };
            }
        }

        public Response<List<Requests>> FindAllRequestsRaisedBy(string username)
        {
            var response = new Response<List<Requests>>();
            var userResponse = new UsersOperator().FindUser(username);
            if (userResponse.IsSuccess)
            {
                return this.FindAllRequestsRaisedBy(userResponse.Item.Id);
            }
            else
            {
                response.IsSuccess = false;
                response.Message = userResponse.Message;
                response.Item = null;
                return response;
            }
        }

        public Response<List<Requests>> FindAllRequestsRaisedBy(int userId)
        {
            var response = new Response<List<Requests>>();

            try
            {
                var q = context.Users.Where(ooxx => ooxx.Id == userId).SingleOrDefault();
                if (null != q)
                {
                    var requests = q.RaisedByMeRequests.ToList();

                    response.IsSuccess = true;
                    response.Message = requests.Count.ToString();
                    response.Item = requests ?? new List<Requests>();
                }
                else
                {
                    response.IsSuccess = false;
                    response.Message = "user not found.";
                    response.Item = null;
                }
            }
            catch (Exception ex)
            {
                response.IsSuccess = false;
                response.Message = "shit happens, " + ex.Message;
                response.Item = null;
            }

            return response;
        }

        public Response<List<Requests>> FindAllRequestsAssignedTo(string username)
        {
            var response = new Response<List<Requests>>();
            var userResponse = new UsersOperator().FindUser(username);
            if (userResponse.IsSuccess)
            {
                return this.FindAllRequestsAssignedTo(userResponse.Item.Id);
            }
            else
            {
                response.IsSuccess = false;
                response.Message = userResponse.Message;
                response.Item = null;
                return response;
            }
        }

        public Response<List<Requests>> FindAllRequestsAssignedTo(int userId)
        {
            var response = new Response<List<Requests>>();
            try
            {
                var q = context.Users.Where(ooxx => ooxx.Id == userId).SingleOrDefault();
                if (null != q)
                {
                    var requests = q.AssignedToMeRequests.ToList();
                    response.Item = requests ?? new List<Requests>();
                    response.IsSuccess = true;
                    response.Message = requests.Count.ToString();
                }
                else
                {
                    response.IsSuccess = false;
                    response.Message = "user not found.";
                    response.Item = null;
                }
            }
            catch (Exception ex)
            {
                response.IsSuccess = false;
                response.Message = "shit happens, " + ex.Message;
                response.Item = null;
            }

            return response;
        }
    }
}
